// pages/forget/forget.js
// pages/register/register.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    o_type: 'possword',
    o_icon: 'icon-icon-test',
    t_type: 'possword',
    t_icon: 'icon-icon-test',
    reg_hqyzm: "获取验证码",
    countdown_time: 60,
    showtime1: true,
    showtime2: false,
    user_type: 1,
    phone: '',
    code: ''
  },
  inputPhone:function(e){
    var phone= e.detail.value;
    this.setData({
      phone:phone
    })
  },
  reg_yanzhengma_huoqu: function () {
    var that = this;
    var phone = that.data.phone;
    util.commonRequest('verification', 'post', { phone: phone }, function (res) {
      if (res.data.code == 200) {
        that.setData({
          showtime1: false,
          showtime2: true,
        });
        countdown(that);
        wx.showToast({
          title: '发送成功',
        })

      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });


  },

  o_swich() {
    var that = this;
    var types = that.data.o_type;
    console.log(types)
    var icon = that.data.o_icon;
    if (types == 'password') {
      that.setData({
        o_type: 'text',
        o_icon: 'icon-xianshikejian'
      })
    } else {
      that.setData({
        o_type: 'password',
        o_icon: 'icon-icon-test'
      })
    }
  },
  t_swich() {
    var that = this;
    var types = that.data.t_type;
    console.log(types)
    var icon = that.data.t_icon;
    if (types == 'password') {
      that.setData({
        t_type: 'text',
        t_icon: 'icon-xianshikejian'
      })
    } else {
      that.setData({
        t_type: 'password',
        t_icon: 'icon-icon-test'
      })
    }
  },
  submit(e) {
    var that = this;
    var phone = e.detail.value.phone;
    var auth = e.detail.value.auth;
    if (!phone) {
      wx.showToast({
        title: '手机号不能为空',
        icon:'none'
      })
      return false;
    } if (!auth) {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
      return false;
    } else if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      let param = {
        phone: phone,
        yacode: auth,
        user_type: that.data.user_type
      };
      util.commonRequest('checkCode', 'post', param, function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '验证成功，设置密码',
            icon: 'success',
            duration: 1500
          })
          setTimeout(function () {				//确认之后跳转的页面
            wx.navigateTo({
              url: '../possword/possword?type=' + that.data.user_type + "&phone=" + phone + "&f=1",
            })
          }, 1500);
        } else {
          wx.showToast({
            icon: 'loading',
            title: res.data.msg,
          })
        }
      });
      // wx.showToast({
      //   title: '验证成功，设置密码',
      //   icon: 'success',
      //   duration: 1500
      // })
      // setTimeout(function () {				//确认之后跳转的页面
      //   wx.navigateTo({
      //     url: '../possword/possword?type=' + that.data.user_type + "&phone=" + phone+"&f=1",
      //   })
      // }, 1500);
    }
  },
  // reg_yanzhengma_huoqu: function () {
  //   console.log("004");
  //   this.setData({
  //     showtime1: false,
  //     showtime2: true,
  //   });
  //   countdown(this);
  //   // wxTimer.start(this); 
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type;
    this.setData({
      user_type:type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function countdown(that) {
  var second = that.data.countdown_time
  if (second == 0) {
    console.log("001");
    that.setData({
      showtime1: true,
      showtime2: false,
      reg_hqyzm: "重新获取",
      countdown_time: 60,
    });
    console.log("002");
    return;
  }
  console.log("003");
  var time = setTimeout(function () {
    that.setData({
      countdown_time: second - 1
    });
    countdown(that);
  }
    , 1000)
}